#include "io_out.h"
#include "modbus.h"
#include "r_cg_timer.h"

static void func1(void)
{
    static uint8_t index = 0;
    static uint32_t lastTime = 0;
    static int8_t  turn = 1;
    
    if(getSysTime() - lastTime >= 250)
    {
        lastTime += 250;
    }else return;
    
    if(turn == 1)
    {
        if(index >= OUTPUT_SIZE - 1)    turn = -1;
        SetGPIO(index++);
    }else if(turn == -1)
    {
        if(index == OUTPUT_SIZE)    index--;
        if(index == 0)   
        {
            turn = 1;
            ResetGPIO(index);
            return;
        }
        ResetGPIO(index--);
    }
    MB_SET(Config_A, A01, index); 
}


void SetGPIO(out_index_t gpiox)
{
    switch(gpiox)
    {
        case OUT_1:	   OUT1 = 	1;   break;
        case OUT_2:	   OUT2 = 	1;   break;
        case OUT_3:	   OUT3 = 	1;   break;
        case OUT_4:	   OUT4 = 	1;   break;
        case OUT_5:	   OUT5 = 	1;   break;
        case OUT_6:	   OUT6 = 	1;   break;
        case OUT_7:	   OUT7 = 	1;   break;
        case OUT_8:	   OUT8 = 	1;   break;
        case OUT_9:	   OUT9 = 	1;   break;
        case OUT_10:   OUT10 = 	1;   break;
        case OUT_11:   OUT11 = 	1;   break;
        case OUT_12:   OUT12 = 	1;   break;
        case OUT_13:   OUT13 = 	1;   break;
        case OUT_14:   OUT14 = 	1;   break;
        case OUT_15:   OUT15 = 	1;   break;
        case OUT_16:   OUT16 = 	1;   break;
        case OUT_17:   OUT17 = 	1;   break;
        case OUT_18:   OUT18 = 	1;   break;
        case OUT_19:   OUT19 = 	1;   break;
        case OUT_20:   OUT20 = 	1;   break;
        case OUT_21:   OUT21 = 	1;   break;
        case OUT_22:   OUT22 = 	1;   break;
        case OUT_23:   OUT23 = 	1;   break;
        case OUT_24:   OUT24 = 	1;   break;
        case OUT_25:   OUT25 = 	1;   break;
    }
}

void ResetGPIO(out_index_t gpiox)
{
    switch(gpiox)
    {
        case OUT_1:	   OUT1 = 	0;   break;
        case OUT_2:	   OUT2 = 	0;   break;
        case OUT_3:	   OUT3 = 	0;   break;
        case OUT_4:	   OUT4 = 	0;   break;
        case OUT_5:	   OUT5 = 	0;   break;
        case OUT_6:	   OUT6 = 	0;   break;
        case OUT_7:	   OUT7 = 	0;   break;
        case OUT_8:	   OUT8 = 	0;   break;
        case OUT_9:	   OUT9 = 	0;   break;
        case OUT_10:   OUT10 = 	0;   break;
        case OUT_11:   OUT11 = 	0;   break;
        case OUT_12:   OUT12 = 	0;   break;
        case OUT_13:   OUT13 = 	0;   break;
        case OUT_14:   OUT14 = 	0;   break;
        case OUT_15:   OUT15 = 	0;   break;
        case OUT_16:   OUT16 = 	0;   break;
        case OUT_17:   OUT17 = 	0;   break;
        case OUT_18:   OUT18 = 	0;   break;
        case OUT_19:   OUT19 = 	0;   break;
        case OUT_20:   OUT20 = 	0;   break;
        case OUT_21:   OUT21 = 	0;   break;
        case OUT_22:   OUT22 = 	0;   break;
        case OUT_23:   OUT23 = 	0;   break;
        case OUT_24:   OUT24 = 	0;   break;
        case OUT_25:   OUT25 = 	0;   break;
    }

}

void OutPutEventLoop(void)
{
    //func1();
}
/*
static uint16_t g_OutPut0 = 0;
static uint16_t g_OutPut1 = 0;

void SetGPIO(uint8_t type, uint8_t val)
{
    uint16_t *t;
    if(type == 0)
        t = &g_OutPut0;
    else
        t = &g_OutPut1;
     *t |= val;   
     MB_SET(Config_A, A03, *t);
}

void ResetGPIO(uint8_t type, uint8_t val)
{
    uint16_t *t;
    if(type == 0)
        t = &g_OutPut0;
    else
        t = &g_OutPut1;
    *t = ~*t; 
    *t |= val;
    *t = ~*t;
    
   
}
*/
